<template>
  <div>
    <!--    搜索图书信息-->
    <div id="search" style="text-align: right">
      <el-form :inline="true" id="demo-form-inline">
        <el-form-item label="图书名称">
          <el-input @keyup.enter.native="handleSearch" v-model="formData.name" placeholder="请输入图书名称"></el-input>
        </el-form-item>
        <el-form-item label="图书作者" style="margin-left: 100px;">
          <el-input @keyup.enter.native="handleSearch" v-model="formData.author" placeholder="请输入图书作者"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 100px;margin-right: 150px;">
          <el-button style="width: 150px;" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <!--  展示图书信息-->
    <div id="list">
      <el-table :data="bookList" style="width: 100%" :stripe="true" :border="true" height="445">
        <el-table-column label="图书编号" width="80px" prop="id"></el-table-column>
        <el-table-column label="图书名称" min-width="150px" prop="name"></el-table-column>
        <el-table-column label="图书作者" min-width="150px" prop="author"></el-table-column>
        <el-table-column label="图书价格(元)" width="120px">
          <template v-slot="scope">
            {{ (scope.row.price) / 100 }}
          </template>
        </el-table-column>
        <el-table-column label="出版日期" min-width="150px" prop="release_date"></el-table-column>
        <el-table-column label="图书封面" min-width="200px">
          <template v-slot="scope">
            <!--           <el-image :header="headerObj" :src="picPath+scope.row.pic_path"></el-image>-->
            <img style="width: 100px;height: 100px" :lazy="true" fit="fill" :src="picPath+scope.row.pic_path" class="avatar">
          </template>
        </el-table-column>
        <el-table-column label="图书库存" min-width="100px" prop="stock"></el-table-column>
        <el-table-column align="center" label="操作" width="250px" fixed="right">
          <template v-slot="scope">
            <el-button size="mini" @click="change(scope.row.id)">修改</el-button>
            <el-button size="mini" type="success" @click="change(scope.row.id)">借出</el-button>
            <el-button size="mini" type="danger" @click="open(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    修改图书信息-->
    <el-dialog :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="图书编号" :label-width="formLabelWidth">
          <el-input placeholder="请输入内容" v-model="book.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="图书名称" :label-width="formLabelWidth">
          <el-input v-model="book.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书作者" :label-width="formLabelWidth">
          <el-input v-model="book.author" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书价格(元)" :label-width="formLabelWidth">
          <el-input type="number" v-model="book.price"></el-input>
        </el-form-item>
        <el-form-item label="出版日期" :label-width="formLabelWidth">
          <el-col :span="11">
            <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                            v-model="book.release_date"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="图书封面" :label-width="formLabelWidth">
          <el-upload class="avatar-uploader" :action="uploadUrl" list-type="picture-card"
                     :show-file-list="false" :on-success="handleAvatarSuccess" name="pic">
            <img v-if="book.pic_path" style="width: 146px;height: 146px" :src="picPath+book.pic_path" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="图书库存" :label-width="formLabelWidth">
          <el-input :disabled="true" v-model="book.stock" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--    分页-->
    <div id="block">
      <el-pagination background
                     :current-page.sync="pagination.page"
                     :page-size="pagination.pageSize"
                     :total="pagination.total"
                     :page-sizes="[7,15,30]"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     layout="total, prev, pager, next, sizes">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import bookApi from "@/api/book";
import qs from "qs";

export default {
  name: "list",
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + '/book/uploadPic',
      bookList: [],
      formData: {
        name: '',
        author: '',
      },
      book: {
        id: '',
        name: '',
        author: '',
        price: '',
        release_date: '',
        pic_path: '',
        stock: '',
      },
      pagination: {
        total: 0,
        page: 1,
        pageSize: 7
      },
      dialogFormVisible: false,
      formLabelWidth: '120px',
      picPath: process.env.VUE_APP_BASE_API + "/images/",
    }
  },
  methods: {
    //图片上传成功回调函数
    handleAvatarSuccess(response, file, fileList) {
      if (response.code === 6666) {
        this.book.pic_path = response.data.picPath
      }
    },
    //打开确认删除的提示框
    open(id) {
      this.$confirm('此操作将删除该图书, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      })
    },
    //删除图书信息/或者减少库存函数
    handleDelete(id) {
      bookApi.deleteBook(id).then(resp => {
        if (resp.code === 6666) {
          this.$message.success('删除成功!');
          this.queryBook();
        } else {
          this.$message.info('删除失败')
        }
      }).catch(error => {
        this.$message.error('删除出错');
      })
    },
    //分页查询图书信息
    queryBook() {
      const pagination = {
        name: this.formData.name,//搜索框中的值
        author: this.formData.author,
        page: this.pagination.page,//当前页码
        pageSize: this.pagination.pageSize,//每页多少条数据
      }
      bookApi.getPageBookList(pagination).then(resp => {
        if (resp.code === 6666) {
          this.bookList = resp.data;
          this.pagination.page = resp.pageInfo.page;
          this.pagination.pageSize = resp.pageInfo.pageSize;
          this.pagination.total = resp.pageInfo.total;
        } else {
          this.$message.info('获取院系列表失败')
        }
      }).catch(() => {
        this.$message.error("查询院系列表出错！")
      })
    },
    //点击查询时回到第一页
    handleSearch() {
      this.pagination.page = 1;
      this.queryBook()
    },
    //页码改变时的回调函数
    handleCurrentChange(page) {
      this.pagination.page = page;
      this.queryBook();
    },
    //页面大小改变时的回调函数
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize;
      this.queryBook();
    },
    //修改按钮点击
    change(id) {
      this.selectBookById(id);
      this.dialogFormVisible = true;
    },
    //修改图书信息函数
    handleEdit() {
      bookApi.updateBook(qs.stringify(this.book)).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("修改成功！")
          this.queryBook()
          this.dialogFormVisible = false;
        } else {
          this.$message.info('修改失败')
        }
      }).catch(() => {
        this.$message.error("修改出错")
      });
    },
    //取消修改的提示信息
    handleCancle() {
      this.dialogFormVisible = false;
    },
    //通过id查询图书信息
    selectBookById(id) {
      bookApi.selectBookById(id).then(resp => {
        if (resp.code === 6666) {
          this.book = resp.data;
        } else {
          this.$message.warning('查询图书信息失败')
        }
      }).catch(() => {
        this.$message.error('查询图书信息出错')
      })
    }
  },
  mounted() {
    this.queryBook();
  }
}
</script>

<style scoped>
#list {
  margin-left: 20px;
  margin-right: 20px;
}

#search {
  margin-left: 100px;
  margin-top: 60px;
}

#block {
  margin-top: 20px;
  margin-left: 20px;
}

#demo-form-inline {
  margin-bottom: 0;
}

.el-divider {
  margin: 10px 0;
}
</style>
